<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>进销管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../public/css/font.css">
    <link rel="stylesheet" href="../../public/css/xadmin.css">
    <script src="../../public/js/jquery.min.js"></script>
    <script type="text/javascript" src="../../public/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../public/js/xadmin.js"></script>
    <script>
        var pid_select;
        var currentCount;
        var submit_btn;
        var count_input;
        $(function () {
            pid_select = $("#pid");
            currentCount = $("#currentCount");
            submit_btn = $("#submit_btn");
            count_input = $("#count");
            $.ajax({
                type: "POST",
                url: "/sale/getAllProduct",
                dataType: "json",
                success: function (mydata) {
                    $.each(mydata, function (i) {
                        pid_select.append('<option value="' + mydata[i].pid + '">' + mydata[i].productName + '</option>');
                    })

                    var number;
                    layui.use('form', function () {
                        var form = layui.form;
                        form.on('select', function (data) {
                            currentCount.text("");
                            for (var i = 0; i < mydata.length; i++) {
                                if (mydata[i].pid == data.value) {
                                    currentCount.text("库存：" + mydata[i].quantity);
                                    number = mydata[i].quantity;
                                }
                            }

                        })
                        form.render();
                    });

                    submit_btn.click(function () {
                        if (count_input.val() > number) {
                            layui.use('layer', function () {
                                var layer = layui.layer;
                                layer.alert("库存不足");
                            })
                        } else {
                            var url = parent.parent.location.href;
                            var param = url.substring(url.indexOf("?") + 1).split("&");
                            var uid = param[0].substring(param[0].indexOf("=") + 1);
                            $.ajax({
                                type: "POST",
                                url: "/sale/saleThings",
                                data: "pid=" + pid_select.val() + "&count=" + count_input.val() + "&uid=" + uid,
                                dataType: "json",
                                success: function (data) {
                                    layui.use('layer', function () {
                                        var layer = layui.layer;
                                        layer.alert('购买成功', function () {
                                            parent.layer.close(parent.layer.index);
                                            parent.table.render();
                                        });
                                    });
                                }
                            });
                        }
                    });
                }
            });
        })
    </script>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item">
                <label for="pid" class="layui-form-label">
                    <span class="x-red">*</span>商品名称
                </label>
                <div class="layui-input-inline">
                    <select id="pid" name="pid">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label for="count" class="layui-form-label">
                    <span class="x-red">*</span>数量
                </label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline">
                        <input type="text" id="count" name="count" required="" lay-verify="required"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <label class="layui-form-label" id="currentCount"></label>
            </div>
            <div class="layui-form-item">
                <label for="submit_btn" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <button type="button" id="submit_btn" class="layui-btn">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    layui.use('form', function () {
        var form = layui.form;
    });

</script>
</html>